
package com.srwl.mytx.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.srwl.easeui.adapter.ContactAdapter;
import com.srwl.easeui.utils.EaseCommonUtils;
import com.srwl.easeui.widget.EaseSidebar;
import com.srwl.mytx.Constant;
import com.srwl.mytx.domain.Message;
import com.srwl.mytx.domain.User;
import com.srwl.mytx.Helper;
import com.srwl.mytx.R;
import com.srwl.mytx.im.ConversationService;
import com.srwl.mytx.im.IMSClientBootstrap;
import com.srwl.mytx.im.conversation.Conversation;
import com.srwl.mytx.parse.UserProfileManager;
import com.srwl.mytx.widget.ConfirmDialog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class ForwardMessageActivity extends BaseActivity {

    private ListView listView;
    private EaseSidebar sidebar;
    protected ContactAdapter contactAdapter;
    private List<User> contactList;
    private User selectUser;
    private Message forwardMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.em_activity_pick_contact_no_checkbox);
        forwardMessage = getIntent().getParcelableExtra(Constant.EXTRA_INFO_FORWARD_MESSAGE);
        listView = findViewById(R.id.list);
        sidebar = findViewById(R.id.sidebar);
        sidebar.setListView(listView);
        contactList = new ArrayList<>();
        // get contactlist
        getContactList();
        // set adapter
        contactAdapter = new ContactAdapter(this, R.layout.ease_row_contact, contactList);
        listView.setAdapter(contactAdapter);
        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                onListItemClick(position);
            }
        });

    }


    protected void onListItemClick(int position) {
        selectUser = contactAdapter.getItem(position);


        ConfirmDialog dialog = new ConfirmDialog(this, "提示", getString(R.string.confirm_forward_to, selectUser.getUsername()), true);
        dialog.setConfirmListener(new ConfirmDialog.ConfirmListener() {
            @Override
            public void confirm() {
                ConversationService conversationService1 = IMSClientBootstrap.getInstance().getConversationService();

                Conversation conversation = conversationService1.getC2cConversation(selectUser.getuId());
                String loginUserID = UserProfileManager.getInstance().getLoginUserInfo().getuId();


                //文件类型的消息需要
                switch (forwardMessage.getSubType()) {
                    case Constant.IM_TYPE_IMAGE:
                    case Constant.IM_TYPE_AUDIO:
                    case Constant.IM_TYPE_VIDEO:
//                        forwardMessage.setUploadState(Constant.MESSAGE_UPLOAD_STATE_SUCCESS);
//                        forwardMessage.setDownloadState(null);//清空下载状态
//                        //如果该消息是发出的消息（相对接收消息而言）,判断是否发送成功
//                        if (EaseCommonUtils.isSendMessage(forwardMessage, loginUserID) && forwardMessage.getSendState() != Constant.IM_SEND_STATE_SUCCESS) {
//                            showToast("非发送成功的消息，不能转发");
//                            return;
//                        }

                }
                forwardMessage.setFromId(loginUserID);
                conversation.sendMessage(forwardMessage);
                Toast.makeText(ForwardMessageActivity.this, "已经转发给" + selectUser.getUsername(), Toast.LENGTH_LONG).show();
                //  finish();

            }
        });

        dialog.show();

//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        builder.setTitle("提醒");
//        builder.setMessage(getString(R.string.confirm_forward_to, selectUser.getUsername()));
//        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//
//                ConversationService conversationService1 = IMSClientBootstrap.getInstance().getConversationService();
//
//                Conversation conversation = conversationService1.getC2cConversation(selectUser.getuId());
//                String loginUserID = UserProfileManager.getInstance().getLoginUserInfo().getuId();
//
//
//                //文件类型的消息需要
//                switch (forwardMessage.getSubType()) {
//                    case Constant.IM_TYPE_IMAGE:
//                    case Constant.IM_TYPE_AUDIO:
//                    case Constant.IM_TYPE_VIDEO:
////                        forwardMessage.setUploadState(Constant.MESSAGE_UPLOAD_STATE_SUCCESS);
////                        forwardMessage.setDownloadState(null);//清空下载状态
////                        //如果该消息是发出的消息（相对接收消息而言）,判断是否发送成功
////                        if (EaseCommonUtils.isSendMessage(forwardMessage, loginUserID) && forwardMessage.getSendState() != Constant.IM_SEND_STATE_SUCCESS) {
////                            showToast("非发送成功的消息，不能转发");
////                            return;
////                        }
//
//                }
//                forwardMessage.setFromId(loginUserID);
//                conversation.sendMessage(forwardMessage);
//
//
//            }
//        });
//        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                dialog.dismiss();
//            }
//        });
//        builder.create().show();


    }

    public void back(View view) {
        finish();
    }

    private void getContactList() {
        contactList.clear();
        Map<String, User> users = Helper.getInstance().getMapContactList();
        Iterator<Entry<String, User>> iterator = users.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry<String, User> entry = iterator.next();
            if (!entry.getKey().equals(Constant.NEW_FRIENDS_USERNAME) && !entry.getKey().equals(Constant.GROUP_USERNAME) && !entry.getKey().equals(Constant.CHAT_ROOM) && !entry.getKey().equals(Constant.CHAT_ROBOT))
                contactList.add(entry.getValue());
        }
        // sort
        Collections.sort(contactList, new Comparator<User>() {

            @Override
            public int compare(User lhs, User rhs) {
                String userName1 = lhs.getUsername() != null ? lhs.getUsername() : lhs.getNick();
                String userName2 = rhs.getUsername() != null ? rhs.getUsername() : rhs.getNick();
                String l1 = EaseCommonUtils.getInitialLette(userName1);
                String l2 = EaseCommonUtils.getInitialLette(userName2);

                if (l1.equals(l2)) {
                    return userName1.compareTo(userName2);
                } else {
                    if ("#".equals(l1)) {
                        return 1;
                    } else if ("#".equals(l2)) {
                        return -1;
                    }
                    return l1.compareTo(l2);
                }

            }
        });
    }

}
